import { ref, reactive } from 'vue';
import { defineStore } from 'pinia';
import Cookies from 'js-cookie';
import { UserInfosStates } from './interface';
import { Local } from '/@/utils/storage';

// 本地缓存
// const data = storage.info();
// console.log(data);

export const smokeStore = defineStore('smoke', function () {
	// 设置设备参数
	const deviceParams: any = reactive({});
	function setDeviceParams(key: string, value: object) {
		deviceParams[key] = value;
	}

	function clearDeviceParams() {
		let keys: any = Object.keys(deviceParams);
		keys.forEach((item: any) => {
			delete deviceParams[item];
		});
	}

	// 设置图片
	const deviceImgs: any = reactive({});
	function setDeviceImgs(device: any) {
		deviceImgs[device.deviceCode] = device.url;
	}

	function clearImgs() {
		let keys: any = Object.keys(deviceImgs);
		keys.forEach((item: any) => {
			delete deviceImgs[item];
		});
	}

	// 设置ws报警消息
	const alarmMsg: any = ref([]);
	function setAlarmMsg(msg: any) {
		alarmMsg.value = msg || [];
	}

	// 根据eventId移除消息
	function removeAlarmMsg(eventId: string) {
		alarmMsg.value = alarmMsg.value.filter((item: any) => {
			return item.eventId != eventId;
		});
	}

	// 设置是否自动弹出报警弹窗
	const autoPopup = ref(Local.get('autoPopup') || 0);
	function setAutoPopup(value: number) {
		autoPopup.value = value;
		Local.set('autoPopup', value);
	}

	// 组织
	const orgHost: any = ref(Local.get('orgHost'));
	function setOrgHost(value: any) {
		orgHost.value = value;
	}

	// 全局ws
	const wsConn: any = ref(null);
	const wsStatus: any = ref(false);

	function setWsConn(value: any) {
		wsConn.value = value;
		setWsStatus(true);
	}

	function setWsStatus(value: boolean) {
		wsStatus.value = value;
	}

	return {
		deviceParams,
		setDeviceParams,
		clearDeviceParams,
		deviceImgs,
		setDeviceImgs,
		clearImgs,
		alarmMsg,
		setAlarmMsg,
		removeAlarmMsg,
		autoPopup,
		setAutoPopup,
		orgHost,
		setOrgHost,
		wsConn,
		setWsConn,
		wsStatus,
		setWsStatus,
	};
});
